package com.haoma.controller;

import com.haoma.annotation.RequestCount;
import com.haoma.entity.Link;
import com.haoma.result.Result;
import com.haoma.service.LinkService;
import com.haoma.vo.LinkApplyForVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * Description: 友链相关接口
 */
@RestController
@RequestMapping("/web/link")
@CrossOrigin

public class LinkController {


    @Autowired
    private LinkService linkService;

    /**
     * 申请友链
     *
     * @return
     */
    @RequestCount
    @PostMapping("/applylink")
    public Result applyForLink(@RequestBody @Validated Link link) {
        String message = linkService.applyForLink(link);
        return Result.success(message);
    }


    /**
     * 上传友链头像
     *
     * @param multipartFile
     * @return
     */
    @RequestCount
    @PostMapping("/fileUpload")
    public Result fileUpload(@RequestParam("file") MultipartFile multipartFile) {
        String fileUrl = linkService.fileUpload(multipartFile);
        return Result.success(fileUrl);
    }

    /**
     * 查询申请友链消息
     *
     * @param linkAddress
     * @return
     */
    @RequestCount
    @GetMapping("/queryapply")
    public Result queryApply(String linkAddress) {
        LinkApplyForVo linkApplyForVo = linkService.queryApply(linkAddress);
        return Result.success(linkApplyForVo);
    }

    /**
     * 查询所有的友链
     *
     * @return
     */
    @RequestCount
    @GetMapping("/linklist")
    public Result getLinkList() {
        List<Link> linkList = linkService.list();
        return Result.success(linkList);
    }

    /**
     * 修改友链
     *
     * @return
     */
    @RequestCount
    @PutMapping("updateLink")
    public Result update(@RequestBody Link link) {
        linkService.update(link);
        return Result.success();
    }
}
